package com.google.code.qsecurity.client.ioc;

import com.google.code.qsecurity.client.QLibrary;
import com.google.code.qsecurity.client.QClientFactory;
import com.google.code.qsecurity.client.QClientFactoryImpl;
import com.google.code.qsecurity.client.QConstants;
import com.google.code.qsecurity.client.mvp.SecurityActivityManager;
import com.google.code.qsecurity.client.mvp.SecurityActivityMapper;
import com.google.code.qsecurity.client.mvp.StateController;
import com.google.code.qsecurity.client.ui.ForgottenPasswordView;
import com.google.code.qsecurity.client.ui.LoginView;
import com.google.code.qsecurity.client.ui.QSecurityUI;
import com.google.code.qsecurity.client.ui.SecurityViewImpl;
import com.google.code.qsecurity.client.ui.SignupView;
import com.google.code.qsecurity.client.ui.VerificationView;
import com.google.code.qsecurity.shared.QRequest;
import com.google.code.qsecurity.shared.QRequestFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;

public class QSecurityGinModule extends AbstractGinModule {

	@Override
	protected void configure() {
		bind(QClientFactory.class).to(QClientFactoryImpl.class).in(Singleton.class);
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
	}
	
	@Provides
	@Singleton
	public QLibrary createLibrary(QClientFactory clientFactory, EventBus eventBus,
			StateController stateController, SecurityActivityMapper activityMapper,
			SecurityActivityManager activityManager) {
		QLibrary library = new QLibrary(clientFactory, eventBus,
				stateController, activityMapper, activityManager);
		return library;
	}
	
	@Provides
	@Singleton
	public QRequestFactory createRequestFactory(EventBus eventBus) {
		QRequestFactory factory = GWT.create(QRequestFactory.class);
		factory.initialize(eventBus);
		return factory;
	}
	
	@Provides
	public QRequest createRequestContext(QRequestFactory factory) {
		return factory.qRequest();
	}
	
	@Provides
	@Singleton
	public QSecurityUI createUI() {
		return new SecurityViewImpl();
	}
	
	@Provides
	@Singleton
	public LoginView createLoginView(QSecurityUI ui) {
		return ui.getLoginView();
	}
	
	@Provides
	@Singleton
	public SignupView createSignupView(QSecurityUI ui) {
		return ui.getSignupView();
	}
	
	@Provides
	@Singleton
	public VerificationView createVerificationView(QSecurityUI ui) {
		return ui.getVerificationView();
	}
	

	
	@Provides
	@Singleton
	public ForgottenPasswordView createForgottenPasswordView(QSecurityUI ui) {
		return ui.getForgottenPasswordView();
	}
	
	@Provides
	@Singleton
	public QConstants createConstants() {
		return GWT.create(QConstants.class);
	}
	
	@Provides
	@Singleton
	public StateController createController(EventBus eventBus) {
		return new StateController(eventBus);
	}

}
